package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.Coupon;
import com.xinhe.web.entity.QCoupon;
import com.xinhe.web.reposity.CouponRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

@Service
public class CouponService {
    @Autowired
    private CouponRepository couponRepository;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    public QueryResults<Coupon> pageCoupon(String name, int pageNum, int pageSize) {
        QCoupon qCoupon = QCoupon.coupon;
        BooleanBuilder builder = new BooleanBuilder();
        if (!Objects.isNull(name)) {
            builder.and(qCoupon.name.contains(name));
        }
        return queryFactory.select(
                        Projections.bean(
                                Coupon.class,
                                qCoupon.id,
                                qCoupon.name,
                                qCoupon.expireTime,
                                qCoupon.money,
                                qCoupon.satisfyMoney,
                                qCoupon.type,
                                qCoupon.status
                        )
                ).from(qCoupon)
                .where(builder).offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }
    @Transactional
    public Result saveCoupon(Long id,String name, Date expireTime, BigDecimal money, BigDecimal satisfyMoney, String type, String remark) {
        Coupon coupon;
        if (satisfyMoney.compareTo(money) <= 0) {
            return Result.failed(4001,"满足金额必须大于减去金额");
        }
        if (id == null) {
            coupon = new Coupon();
            coupon.setStatus("ENABLE");
            couponRepository.save(coupon);
        } else {
            coupon = couponRepository.findById(id).get();
        }
        coupon.setName(name);
        coupon.setExpireTime(expireTime);
        coupon.setMoney(money);
        coupon.setSatisfyMoney(satisfyMoney);
        coupon.setMoney(money);
        coupon.setType(type);
        couponRepository.saveAndFlush(coupon);
        return Result.ok();
    }
    @Transactional
    public Result saveCouponStatus(Long couponId, String status) {
        Optional<Coupon> byId = couponRepository.findById(couponId);
        if (!byId.isPresent()) {
            Coupon coupon = byId.get();
            coupon.setStatus(status);
            couponRepository.saveAndFlush(coupon);
        }
        return Result.ok();
    }
    public Result delete(Long couponId) {
        couponRepository.deleteById(couponId);
        return Result.ok();
    }
    public Result<Coupon> findById(Long couponId) {
        Coupon coupon = couponRepository.findById(couponId).get();
        return Result.ok(coupon);
    }
}
